package com.carbon.notice.controller;

import com.carbon.notice.entity.QuestionAnswerInfo;
import com.carbon.model.Result;
import com.carbon.notice.service.QuestionAnswerInfoService;
import com.carbon.util.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/questionAnswerInfo")
public class QuestionAnswerInfoController {

    @Autowired
    private QuestionAnswerInfoService questionAnswerInfoService;


     /**
     * @Description 提问
     */
    @PostMapping("/ask")
    public Result ask(@RequestBody QuestionAnswerInfo questionAnswerInfo){
        questionAnswerInfo.setCreateTime(new Date());
        return new Result(questionAnswerInfoService.save(questionAnswerInfo));
    }


     /**
     * @Description 专家回答
     */
    @PostMapping("/answer")
    public Result answer(@RequestBody QuestionAnswerInfo questionAnswerInfo) {
        questionAnswerInfo.setUpdateTime(new Date());
        //设置问题状态为已解决
        questionAnswerInfo.setStatus(Constants.SFDM_S);
        return new Result(questionAnswerInfoService.updateById(questionAnswerInfo));
    }

     /**
     * @Description 根据id获取问题详情
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        return new Result(questionAnswerInfoService.getById(id));
    }

     /**
     * @Description 分页查询
     */
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody QuestionAnswerInfo questionAnswerInfo) {
        PageHelper.startPage(questionAnswerInfo.getPageNum(),questionAnswerInfo.getPageSize());
        List<QuestionAnswerInfo> list = questionAnswerInfoService.queryPage(questionAnswerInfo);
        PageInfo<QuestionAnswerInfo> pageInfo = new PageInfo<>(list);
        return new Result(pageInfo);
    }

}
